######################################
# Generate plots conjoint data
######################################

rm(list=ls())

library("ggplot2")
library(gridExtra)
library(ggpubr)
library(grid)

# set working directory
setwd("~/Dropbox/Cerrillos 2017/08_replication")

####################
# left no likely3
####################

# load data
d <- read.table("001_left_nolikely3.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"

  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  

  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_left_nolikely3 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Unlikely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_left_nolikely3 

####################
# left likely3
####################

# load data
d <- read.table("002_left_likely3.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_left_likely3 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Likely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_left_likely3 

####################
# left difference
####################

# load data
d <- read.table("003_left_likely3_difference.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_left_likely3_difference = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Difference") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_left_likely3_difference

####################
# right no likely3
####################

# load data
d <- read.table("004_right_nolikely3.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_right_nolikely3 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Unlikely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_right_nolikely3 

####################
# right likely3
####################

# load data
d <- read.table("005_right_likely3.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_right_likely3 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Likely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_right_likely3 

####################
# right difference
####################

# load data
d <- read.table("006_right_likely3_difference.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_right_likely3_difference = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Difference") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_right_likely3_difference

####################
# center no likely3
####################

# load data
d <- read.table("007_center_nolikely3.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_center_nolikely3 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Unlikely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_center_nolikely3 

####################
# center likely3
####################

# load data
d <- read.table("008_center_likely3.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_center_likely3 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Likely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_center_likely3 

####################
# center difference
####################

# load data
d <- read.table("009_center_likely3_difference.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_center_likely3_difference = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Difference") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_center_likely3_difference

#########################
# no ideological no likely3
#########################

# load data
d <- read.table("010_noideo_nolikely3.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_noideo_nolikely3 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Unlikely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_noideo_nolikely3 

#######################
# non ideological likely3
#######################

# load data
d <- read.table("011_noideo_likely3.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_noideo_likely3 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Likely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_noideo_likely3 

##############################
# non ideological difference
##############################

# load data
d <- read.table("012_noideo_likely3_difference.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_noideo_likely3_difference = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Difference") + theme(plot.title = element_text(size=20, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_noideo_likely3_difference

#############################
# Appendix D
#############################

f1 = grid.arrange(f_left_likely3,f_left_nolikely3,f_left_likely3_difference, ncol = 3, top = textGrob("Panel A: Left-wing respondents",gp=gpar(fontsize=35,fontface = 'bold')))
f2 = grid.arrange(f_right_likely3,f_right_nolikely3,f_right_likely3_difference, ncol = 3,  top = textGrob("Panel B: Right-wing respondents",gp=gpar(fontsize=35,fontface = 'bold')))
f3 = grid.arrange(f_center_likely3,f_center_nolikely3,f_center_likely3_difference, ncol = 3,  top = textGrob("Panel C: Centrist respondents",gp=gpar(fontsize=35,fontface = 'bold')))
f4 = grid.arrange(f_noideo_likely3,f_noideo_nolikely3,f_noideo_likely3_difference, ncol = 3,  top = textGrob("Panel D: Non-identifiers",gp=gpar(fontsize=35,fontface = 'bold')))

pdf('~/Dropbox/Cerrillos 2017/08_replication/f_main_likely3.pdf',width = 20,height=25)
ggarrange(f1,f2,f3,f4, nrow = 4)
dev.off()

